
import { request } from '../../request/index.js'
import regeneratorRuntime from '../../lib/runtime/runtime.js'


Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsInfo: [],
    tabs: [
      {
        index: 0,
        value: "全部",
        active: true
      },
      {
        index: 1,
        value: "待付款",
        active: false
      },
      {
        index: 2,
        value: "待发货",
        active: false
      },
      {
        index: 3,
        value: "退款/退货",
        active: false
      },
    ]
  },
  onShow() {
    const taken = wx.getStorageSync("taken");
    if (!taken) {
      wx.navigateTo({
        url: '/pages/auth/auth',
      })
      return
    }
    let goodsInfo = wx.getStorageSync("payGoods")
    goodsInfo = goodsInfo.map(v => ({ ...v, add_time_cn: (new Date(v.add_time * 1000).toLocaleString()) }))
    this.setData({
      goodsInfo
    })
    // 获取页面栈
    let pages = getCurrentPages()
    // 索引最大的就是当前页面
    let currentPages = pages[pages.length - 1]
    const { type } = currentPages.options
    this.handleTitleChange(type - 1)
    this.getOrderList(type)

  },
  // 获取订单列表数据
  async getOrderList(type) {
    const res = await request({ url: '/my/orders/all', data: { type } })
  },
  // 根据标题索引来改变tab
  handleTitleChange(index) {
    let { tabs } = this.data
    tabs.forEach((v, i) => i === index ? v.active = true : v.active = false)
    this.setData({
      tabs
    })
  },
  // 根据tab改变来改变
  tabsIndex(e) {
    const { index } = e.detail
    this.handleTitleChange(index)
  }

})